﻿using DotNetNuke.Common.Utilities;
using DotNetNuke.Modules.TaskManager.Data;

namespace DotNetNuke.Modules.TaskManager.Components
{
   using System.Collections.Generic;

   public class TaskController
   {
      public static Task GetTask(int taskId)
      {
         return CBO.FillObject<Task>(DataProvider.Instance().GetTask(taskId));
      }

      public static List<Task> GetTasks(int moduleId)
      {
         return CBO.FillCollection<Task>(DataProvider.Instance().GetTasks(moduleId));
      }

      public static void DeleteTask(int taskId)
      {
         DataProvider.Instance().DeleteTask(taskId);
      }

      public static void DeleteTasks(int moduleId)
      {
         DataProvider.Instance().DeleteTasks(moduleId);
      }

      public static int SaveTask(Task task, int tabId)
      {
         if(task.TaskId < 1)
         {
            task.TaskId = DataProvider.Instance().AddTask(task);

            //content Item integration
            var contentTaxonomy = new Taxonomy.Content();
            var contentItem = contentTaxonomy.CreateContentItem(task, tabId);
            task.ContentItemId = contentItem.ContentItemId;
            SaveTask(task, tabId);
         }
         else
         {
            DataProvider.Instance().UpdateTask(task);
            var contentTaxonomy = new Taxonomy.Content();
            contentTaxonomy.UpdateContentItem(task,tabId);
         }
         return task.TaskId;
      }

   }
}